/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.upload.service.processor;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.upload.service.order.UploadBatchOrder;
import info.peigen.hotpot.component.upload.service.order.UploadOrder;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;

import java.util.Optional;

/**
 * <b>(UploadPathProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/11/14
 */
@SuppressWarnings("rawtypes")
@Slf4j
@Component
public class UploadPathProcessor extends AbstractUploadServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 12;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        if (serviceContext.getOrder() instanceof UploadOrder order) {
            order.setUploadPath(addForwardSlash(order.getUploadPath()));
        }

        if (serviceContext.getOrder() instanceof UploadBatchOrder order) {
            order.setUploadPath(addForwardSlash(order.getUploadPath()));
        }
    }

    private String addForwardSlash(String uploadPath) {
        uploadPath = Optional.ofNullable(uploadPath).orElse(uploadProperties.getUploadPath());
        return StrUtil.endWith(uploadPath, FileUtil.FILE_SEPARATOR) ? uploadPath : uploadPath + FileUtil.FILE_SEPARATOR;
    }

}